package arithmetic.leetCode.yi;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yilin
 * Date: 2019-09-03
 * Time: 21:15
 */
public class FirstUniqChar {

    public int firstUniqChar(String s) {
        int length = s.length();
        Map<Character, Integer> counts = new HashMap<>();
        for (int i = 0; i < length; i++) {
            char key = s.charAt(i);
            counts.put(key, counts.getOrDefault(key, 0) + 1);
        }
        for (int i = 0; i < length; i++) {
            if (counts.get(s.charAt(i)) == 1) {
                return i;
            }
        }
        return -1;
    }
}
